<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" >
				<el-form-item>
					<el-input v-model="keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="keywordQuery">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表

		  @selection-change 复选框的点击事件
		-->
		<el-table :data="details" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="产品名称" width="120" sortable>
			</el-table-column>
			<el-table-column prop="type.name" label="产品分类" width="220" sortable>
			</el-table-column>
			<el-table-column label="操作">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next"
						   @current-change="handleCurrentChange"
						   :page-size="pageSize"
						   :total="totals"
						   :current-page="currentPage"
						   style="float:right;">
			</el-pagination>
		</el-col>

		<!--新增/编辑界面-->
		<el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
			<el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
				<el-form-item label="产品名称" prop="name">
					<el-input v-model="saveForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="类型">
					<el-select v-model="saveForm.types_id" placeholder="请选择你的打工场所">
						<el-option v-for="item in typee"
								   :label="item.name"
								   :value="item.id">
							<span style="float: left">{{ item.name }}</span>
							<span style="float: right; color: #8492a6; font-size: 13px">{{ item.sn }}
                    </span>
						</el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
    import util from '../../common/js/util'
    //import NProgress from 'nprogress'
    import { getDetailListPage, removeDetail, batchRemoveDetail, editDetail, saveDetail } from '../../api/api';

    export default {
        data() {
            return {
                title:"",
				keyword: '',

                details: [],
                typee:[],//管理员下拉列表
                deptTree:[],
                totals: 0,
                currentPage: 1,
                pageSize:5,

                listLoading: false,
                sels: [],//列表选中行

                saveFormVisible: false,//新增界面是否显示
                saveLoading: false,
                saveFormRules: {
                    name: [
                        { required: true, message: '请输入姓名你个傻吊', trigger: 'blur' }
                    ],
                    sn: [
                        { required: true, message: '请输入编号你个傻吊', trigger: 'blur' }
                    ]
                },
                //新增界面数据
                saveForm: {
                    id:null,
                    name: '',
                    types_id: ''
                }
            }
        },
        methods: {
            keywordQuery(){//做高级查询,一定要定位到第一页
                this.currentPage = 1;
                this.getDetails();
            },
            handleCurrentChange(val) {
                this.currentPage = val;//点击页码之后给模型层赋值
                this.getDetails();
            },
            //获取部门列表
            getDetails() {
                let para = {
                    currentPage: this.currentPage,
                    pageSize: this.pageSize,
                    keyword: this.keyword
                };

                this.listLoading = true;//开启忙等框
                //NProgress.start();
                //发送axios请求从后端  8080 拿数据
                this.$http.post("/deta",para).then(res=>{
                    //给employees数组赋值
                    this.details = res.data.data;
                    //给分页赋值
                    this.totals = res.data.totals;
                    //关闭加载框
                    this.listLoading = false;
                }).catch(res=>{
                    this.$message.error("系统繁忙，正在骚扰程序猿")
                })
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //用ajax发送请求
                    this.$http.delete("/deta/"+row.id).then(res=>{
                        this.listLoading = false;
                        //判断执行是否成功
                        if (res.data.success){
                            this.$message.success("我们胜利了")
                            //判断当前页面是否有数据
                            if(this.pageSize==0){
                                this.currentPage = this.currentPage-1
                            }
                            this.getDetails()
                        }else {
                            this.$message.error("程序猿要加班了【500】")
                        }
                    }).catch(res=>{
                        this.$message.error("【400.404】警告进入未知区域")
                    })
                }).catch(() => {

                });
            },
            //显示编辑界面
            handleEdit: function (index, row) {
                this.title = "编辑";
                this.geTypee();
                this.saveFormVisible = true;
                this.saveForm = Object.assign({}, row);
            },
            //显示新增界面
            handleAdd: function () {
                this.title = "新增";
                this.geTypee();
                this.saveFormVisible = true;
                this. saveForm= {
                    id:null,
                    name: '',
                    types_id: ''
                };
            },
            //编辑
            saveSubmit: function () {
                this.$refs.saveForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.saveLoading = true;
                            //绑定数据
                            let para = Object.assign({}, this.saveForm);
                            //ajax发送请求
                            this.$http.put("/deta",para).then(res=>{
                                //关闭忙等框
                                this.saveLoading = false;
                                //关闭会话框
                                this.saveFormVisible = false;
                                if (res.data.success){
                                    this.$message.success("我们成功了")
                                    this.getDetails()
                                }else {
                                    this.$message.error("正在枪毙程序猿【500】")
                                }
                            }).catch(res=>{
                                this.$message.error("我要崩溃了【400.404】")
                            })
                        });
                    }
                });
            },
            selsChange: function (sels) {//复选框勾选的时候触发 当前复选框选中的所有行的数据
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id);
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //ajax发送请求
                    this.$http.patch("/deta",ids).then(res=>{
                        this.listLoading = false;
                        //判断成功没
                        if (res.data.success){
                            this.currentPage = 1;
                            this.$message.success("我他妈莱依拉")
                            this.getDetails()
                        }else {
                            this.$message.error("你这是要我去死【500】")
                            this.getDetails()
                        }
                    }).catch(res=>{
                        this.getDetails()
                        this.$message.error("我不活了【400.404】")
                    })
                }).catch(() => {

                });
            },
            geTypee(){
                this.$http.get("/dicType").then(res=>{
                    this.typee = res.data;
                })
            }
        },
        mounted() {
            this.getDetails();
            this.geTypee();
        }
    }

</script>

<style scoped>

</style>